/**
 * 网络请求
 */
import baseURL, {
	storeName
} from "@/config/index"
class Http {
	constructor(params) {
		this.baseURL = params.baseURL
	}

	get(url, data) {
		return this.http('GET', url, data)
	}

	post(url, data) {
		return this.http('POST', url, data)
	}

	http(method, url, data) {
		return new Promise((resolve, reject) => {
			var storeData = uni.getStorageSync(storeName, {})
			var token = storeData.token || {}
			// var userInfo = storeData.userInfo
			// if (token && userInfo && timeStamp < userInfo.expiretime) {
				
			// }
			uni.request({
				url: this.baseURL + url,
				data,
				header: {
					Accept: 'application/json',
					'Content-Type': 'application/json',
					'token': token.token
				},
				method,
				success: ({
					data: res
				}) => {
					if (res.msg === '请先登陆') {
						uni.clearStorageSync()
						uni.reLaunch({
							url: '/pages/login/index'
						})
						return
					}
					if (res.code != 200 && res.code != 1 && res.msg !== '购物车空空如也' && res.msg !== '查询失败') {
						uni.showToast({
							title: res.msg,
							icon: 'none',
						})
					}
					resolve(res)
				},
				fail: (error) => {
					reject(error)
				},
				complete: () => {}
			})
		})
	}
}

const http = new Http({
	baseURL
})

export default http